package cn.tx.controller;


import cn.tx.model.Clazz;
import cn.tx.model.Grade;
import cn.tx.model.GradeStudent;
import cn.tx.model.GradeYear;
import cn.tx.page.Page;
import cn.tx.service.ClazzService;
import cn.tx.service.GradeService;
import cn.tx.service.GradeStudentService;
import cn.tx.service.GradeYearService;
import cn.tx.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping(path = "/gradestudent")
public class GradeStudentController {

    @Autowired
    private GradeStudentService gradeStudentService;
    @Autowired
    private GradeYearService gradeYearService;
    @Autowired
    private ClazzService clazzService;
    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("grade/gradestudent_list");
        List<GradeYear> list = gradeYearService.findAll();
        modelAndView.addObject("gadeYearJson", JSONArray.fromObject(list));
        return modelAndView;
    }
    /*报表展示*/
    @RequestMapping(path = "/report")
    public ModelAndView report(ModelAndView modelAndView) {
        modelAndView.setViewName("grade/report");
        return modelAndView;
    }
    @RequestMapping(path = "/reportlist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> reportlist(GradeStudent grade) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        querymap.put("syear",grade.getSyear());
        querymap.put("eyear",grade.getEyear());
        List<GradeStudent> list = gradeStudentService.findByReportList(querymap);
        String str = "";
        for(GradeStudent st:list){
            str +="['"+st.getName()+"', "+st.getCount()+"],";
        }
        if(!"".equals(str))
            map.put("date", str.substring(0,str.length()-1));
        else
            map.put("date","");
        return map;
    }
    /*选择人员展示*/
    @RequestMapping(path = "/selectStudent")
    public ModelAndView selectStudent(ModelAndView modelAndView) {
        modelAndView.setViewName("grade/selectStudent");
        List<Clazz> clazzList = clazzService.findAll();
        modelAndView.addObject("clazzList", clazzList);
        modelAndView.addObject("clazzListJson", JSONArray.fromObject(clazzList));

        List<GradeYear> list = gradeYearService.findAll();
        modelAndView.addObject("gadeYearJson", JSONArray.fromObject(list));
        return modelAndView;
    }

    /*展示年级列表*/
    @RequestMapping(path = "/get_list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            Page page
    ) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        querymap.put("name","%"+name+"%");
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows",gradeStudentService.findList(querymap));
        map.put("total",gradeStudentService.findTotal(querymap));
        return map;
    }



    /*添加年级*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(GradeStudent grade) {
        Map<String, String> map = new HashMap<>();
        if (grade == null) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        if (StringUtils.isEmpty(grade.getName())) {
            map.put("type", "error");
            map.put("msg", "年级名不能为空");
            return map;
        }

       /* GradeStudent userName = gradeStudentService.findByUserName(grade.getName());
        if (userName!=null){
            map.put("type", "error");
            map.put("msg", "年级名已存在");
            return map;
        }*/
        if (gradeStudentService.addGrade(grade) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }


    /*编辑年级*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(GradeStudent grade) {
        Map<String, String> map = new HashMap<>();
        GradeStudent userName = gradeStudentService.findByUserName(grade.getName());
        if (userName!=null){
            if (grade.getId()!=userName.getId()){
                map.put("type", "error");
                map.put("msg", "id不匹配");
                return map;
            }
        }
        System.out.println(grade);
        if (gradeStudentService.UpdateGradeById(grade)<= 0) {
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功");
        return map;
    }


    /*删除年级*/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "ids[]",required = true) Long[] ids
    ) {
        Map<String, String> map = new HashMap<>();
        if (ids==null||ids.length==0){
            map.put("type", "error");
            map.put("msg", "请至少选择一条数据");
            return map;
        }
        try {
            if(gradeStudentService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                map.put("type", "error");
                map.put("msg", "删除失败！");
                return map;
            }
        } catch (Exception e) {
            map.put("type", "error");
            map.put("msg", "该年级下存在班级信息，请勿冲动！");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功");
        return map;
    }



}
